﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FortuneHeavenBAL;
using FortuneHeavenCommon.Enum;
using System.Text;
using FortuneHeavenCommon.DataMap;

namespace FortuneHeaven.Web.Pages
{
    /// <summary>
    /// ContentHandler 的摘要说明
    /// </summary>
    public class ContentHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.Clear();
            if (context.Request["newstype"] != null)
            {
                NewsType type = (NewsType)Enum.Parse(typeof(NewsType), context.Request["newstype"]);
                context.Response.Write(GetNews(6,type));
            }
            if (context.Request["toplist"] != null)
            {
                int listtype = int.Parse(context.Request["toplist"]);
                context.Response.Write(GetTopList(6, listtype));
            }
            context.Response.End();
        }

        public static string GetNews(int size, NewsType type) 
        {
            StringBuilder builder = new StringBuilder();
            NewsBO bo = new NewsBO();
            List<FortuneHeavenCommon.DataMap.News> list = bo.GetTopNews(size, type);
            string url = "";
            switch (type)
            {
                case NewsType.BroadCast:
                    url = "Pages/News/NewsDetail.aspx";
                    break;
                case NewsType.Games:
                    url = "Pages/News/GameNewsDetail.aspx";
                    break;
                case NewsType.Competition:
                    url = "Pages/News/CompetitionDetail.aspx";
                    break;
                default:
                    break;
            }
            if (list != null && list.Count > 0)
            {
                for (int index = 0; index < list.Count; index++)
                {
                    if (index < 3)
                    {
                        builder.AppendLine(string.Format(@"<li><span class=""left""><a href=""{0}?newsid={1}"">{2}</a><img src=""Content/Image/Main/icon3.jpg"" /></span><span
                            class=""right""><a href=""javascript://"">{3}</a></span></li>", url, list[index].ID, GetTitle(list[index].Title), string.Format("{0:MM-dd}", list[index].Time)));
                    }
                    else
                    {
                        builder.AppendLine(string.Format(@"<li><span class=""left""><a href=""{0}?newsid={1}"">{2}</a></span><span
                            class=""right""><a href=""javascript://"">{3}</a></span></li>", url, list[index].ID, GetTitle(list[index].Title), string.Format("{0:MM-dd}", list[index].Time)));
                    }
                }
            }
            return builder.ToString();
        }

        public static string GetTopList(int size, int type)
        {

            switch (type)
            {
                case 1:
                    return GetTreasueList(size);
                case 2:
                    return GetExp(size);
                case 3:
                    return GetSpread(size);
            }
            return "";
        }


        private static string GetSpread(int size)
        {

            StringBuilder builder = new StringBuilder();
     
            UserInforBO bo = new UserInforBO();
            List<UserInfor> games = bo.GetTopSpreadValue(size);
            if (games == null || games.Count == 0)
                return "";

            UserInfor first = games[0];
            AccountsInfoBO acountbo = new AccountsInfoBO();
            AccountsInfo acount = acountbo.GetAcount(first.UserID);
            int gender = 0;
            if (acount != null)
            {
                gender = acount.Gender; 
            }
            builder.AppendLine("  <div class=\"toux \">");
            builder.AppendLine("   <div class=\"left\">");
            builder.AppendLine("  <a href=\"javascript://\">");
            builder.Append(GetFaceImgUrl(first.FaceID, gender));

            builder.Append(" </a>");
            builder.AppendLine("</div>");
            builder.AppendLine(" <div class=\"right\">");
            builder.AppendLine("  <h3 class=\"weight\">");
            builder.Append(acount.Accounts);
            builder.Append("</h3>");
            builder.Append("<h3>");
            builder.AppendLine("ID:");
            builder.Append(acount.GameID.ToString());
            builder.Append("</h3>");
            builder.Append("<h3>");
            builder.AppendLine("积分:");
            builder.Append(first.SpreadValue.ToString());
            builder.Append("</h3>");
            builder.AppendLine(@" </div>
                        <div class=""clear"">
                        </div>
                    </div>");
            builder.AppendLine(" <ul>");
            for (int index = 1; index < games.Count; index++)
            {
                builder.AppendLine(" <li>");
                builder.AppendLine(" <img src=\"Content/Image/Main/icon.jpg\" /><a href=\"javascript://\">");
                builder.Append(games[index].UserName);
                builder.Append(" 积分:");
                builder.Append(games[index].SpreadValue);
                builder.Append("</a></li>");
            }
            builder.AppendLine(" </ul>");
            return builder.ToString();
        }

        private static string GetExp(int size)
        {

            StringBuilder builder = new StringBuilder();
            AccountsInfoBO bo = new AccountsInfoBO();
            List<AccountsInfo> games = bo.GetTopShortInfor(size);
            if (games == null || games.Count == 0)
                return "";
            AccountsInfo first = games[0];
            UserInforBO userbo = new UserInforBO();
            int faceid =0;
            UserInfor user = userbo.GetUserInfor(first.UserID);
            if (user != null)
            {
                faceid = user.FaceID;
            }
            builder.AppendLine("  <div class=\"toux \">");
            builder.AppendLine("   <div class=\"left\">");
            builder.AppendLine("  <a href=\"javascript://\">");
            builder.Append(GetFaceImgUrl(faceid, first.Gender));

            builder.Append(" </a>");
            builder.AppendLine("</div>");
            builder.AppendLine(" <div class=\"right\">");
            builder.AppendLine("  <h3 class=\"weight\">");
            builder.Append(first.Accounts);
            builder.Append("</h3>");
            builder.Append("<h3>");
            builder.AppendLine("ID:");
            builder.Append(first.GameID.ToString());
            builder.Append("</h3>");
            builder.Append("<h3>");
            builder.AppendLine("魅力值:");
            builder.Append(first.Loveliness.ToString());
            builder.Append("</h3>");
            builder.AppendLine(@" </div>
                        <div class=""clear"">
                        </div>
                    </div>");
            builder.AppendLine(" <ul>");
            for (int index = 1; index < games.Count; index++)
            {
                builder.AppendLine(" <li>");
                builder.AppendLine(" <img src=\"Content/Image/Main/icon.jpg\" /><a href=\"javascript://\">");
                builder.Append(games[index].Accounts);
                builder.Append(" 魅力值:");
                builder.Append(games[index].Loveliness);
                builder.Append("</a></li>");
            }
            builder.AppendLine(" </ul>");
            return builder.ToString();
        }

        private static string GetTreasueList(int size)
        {
            StringBuilder builder = new StringBuilder();
            GameScoreInfoBO bo = new GameScoreInfoBO();
            List<GameScoreInfo> games = bo.GetTopShortInfor(size);
            if (games == null || games.Count == 0)
                return "";
            GameScoreInfo first = games[0];
            UserInforBO userbo = new UserInforBO();
            int faceid = 0;
            UserInfor user = userbo.GetUserInfor(first.UserID);
            if (user != null)
            {
                faceid = user.FaceID;
            }
            builder.AppendLine("  <div class=\"toux \">");
            builder.AppendLine("   <div class=\"left\">");
            builder.AppendLine("  <a href=\"javascript://\">");

            builder.Append(GetFaceImgUrl(faceid, first.Gender));
            builder.Append(" </a>");
            builder.AppendLine("</div>");
            builder.AppendLine(" <div class=\"right\">");
            builder.AppendLine("  <h3 class=\"weight\">");
            builder.Append(first.UserName);
            builder.Append("</h3>");
            builder.Append("<h3>");
            builder.AppendLine("ID:");
            builder.Append(first.GameID.ToString());
            builder.Append("</h3>");
            builder.Append("<h3>");
            builder.AppendLine("堂豆:");
            builder.Append(first.Score.ToString());
            builder.Append("</h3>");
            builder.AppendLine(@" </div>
                        <div class=""clear"">
                        </div>
                    </div>");
            builder.AppendLine(" <ul>");
            for (int index = 1; index < games.Count; index++)
            {
                builder.AppendLine(" <li>");
                builder.AppendLine(" <img src=\"Content/Image/Main/icon.jpg\" /><a href=\"javascript://\">");
                builder.Append(games[index].UserName);
                builder.Append(" 堂豆:");
                builder.Append(games[index].Score);
                builder.Append("</a></li>");
            }
            builder.AppendLine(" </ul>");
            return builder.ToString();
        }

        private static string GetFaceImgUrl(int faceid, int gender)
        {
            return string.Format("   <img src=\"Pages/ImageHandler.ashx?headid={0}&gender={1}\" />", faceid, gender);
            //return "   <img src=\"Content/Image/Main/pic.gif\" />";
        }

        public static string GetTitle(string input)
        {
            if (!string.IsNullOrEmpty(input) && input.Length > 12)
            {
                return string.Format("{0}{1}", input.Substring(0, 10), "...");
            }
            return input;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}